package com.commerce.reader.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;



import com.commerce.reader.Entity.Books;
import com.commerce.reader.R;

import java.util.List;

public class MycardAdapter extends BaseAdapter{

    Context context; List<Books> booksList;
    private final View[] views;

    public MycardAdapter(Context context, List<Books> booksList) {
        this.context=context;
        this.booksList = booksList;
        views = new View[booksList.size()];
    }

    @Override
    public int getCount() {
        return booksList.size();
    }

    @Override
    public Object getItem(int i) {
        return booksList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder{
        private TextView textView;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Log.d("TAG", "getView: " +"++++++" + "-------" + "-------"+ i);
        ViewHolder viewHolder;
        if ( views [i] != null ) {
            return views[ i] ;
        }
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.layout_item,null);
            viewHolder=new ViewHolder();
            viewHolder.textView = view.findViewById(R.id.li_tv_book);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.textView.setText(booksList.get(i).getName());
        views[i]=view;
        return view;
    }
}